﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;

namespace KorteCoConvert
{
    /// <summary>
    /// Main form for KorteCoConvert
    /// </summary>
    public partial class Form1 : Form
    {
        // Variables for storing the properly formatted input and output types.
        private string inputType = "";
        private string outputType = "";
        //Set to false if any conversions fail.
        private bool safeExit = true;
        /// <summary>
        /// Starts a new instance of Form1
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowse = new FolderBrowserDialog();
            folderBrowse.ShowDialog();
            outLabel.Text = folderBrowse.SelectedPath.ToString();
            processBtn.Enabled = true;
            processToolStripMenuItem.Enabled = true;
        }

        //This closes the program
        private void exitBtn_Click(object sender, EventArgs e)
        {
            Close();
        }

        //This adds files to the list
        private void addBtn_Click(object sender, EventArgs e)
        {

            Boolean validType = true;
            OpenFileDialog open = new OpenFileDialog();
            if (inputType.Equals("HPGL"))
                open.Filter = "Plot Files (*.plt)|*.PLT";
            else if (inputType.Equals("PDF"))
                open.Filter = "Portable Document Format Files (*.pdf)|*.PDF";
            else if (inputType.Equals("TIFF"))
                open.Filter = "Tag Image File Format (*.tif)|*.TIF";
            else
            {
                MessageBox.Show("Please select a conversion type.");
                validType = false;
            }
            if (validType && open.ShowDialog() == DialogResult.OK)
            {
                if (!fileBox.Items.Contains(open.FileName))
                    fileBox.Items.Add(open.FileName);
                browseBtn.Enabled = true;
                removeBtn.Enabled = true;
                removeAllBtn.Enabled = true;
                outputToolStripMenuItem.Enabled = true;
            }
        }

        private void addFilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addBtn.PerformClick();
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browseBtn.PerformClick();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void typeSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectionData = typeSelect.SelectedItem.ToString();
            if (selectionData.Length != 0)
            {
                inputType = selectionData.Substring(0, selectionData.IndexOf(" "));
                outputType = selectionData.Substring(selectionData.IndexOf(" to ") + 4, selectionData.Length - selectionData.IndexOf(" to ") - 4);

            }
        }

        private void removeBtn_Click(object sender, EventArgs e)
        {
            if (fileBox.Items.Count != 0)
            {
                for (int num = 0; num <= fileBox.Items.Count; num++)
                {
                    fileBox.Items.Remove(fileBox.SelectedItem);
                }

            }
            if (fileBox.Items.Count == 0)
            {
                outLabel.Text = "";
                removeBtn.Enabled = false;
                browseBtn.Enabled = false;
                processBtn.Enabled = false;
                removeAllBtn.Enabled = false;
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.ShowDialog();
        }

        private void processToolStripMenuItem_Click(object sender, EventArgs e)
        {
            processBtn.PerformClick();
        }

        private void fileBox_DragDrop(object sender, DragEventArgs e)
        {
            //Add filename to an Array.
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            //Loop through the array and add the filename to the listbox
            foreach (string file in files)
            {

                //Only Allows PDF and HPGL File Extentions. Also checks for duplicates.
                if (file.IndexOf("pdf") != -1 && !fileBox.Items.Contains(file) && inputType == "PDF")
                    fileBox.Items.Add(file);

                else if (file.IndexOf("plt") != -1 && !fileBox.Items.Contains(file) && inputType == "HPGL")
                    fileBox.Items.Add(file);

                else if (file.IndexOf("PDF") != -1 && !fileBox.Items.Contains(file) && inputType == "PDF")
                    fileBox.Items.Add(file);

                else if (file.IndexOf("PLT") != -1 && !fileBox.Items.Contains(file) && inputType == "HPGL")
                    fileBox.Items.Add(file);
            }
          if (fileBox.Items.Count > 0)
            {
                browseBtn.Enabled = true;
                removeBtn.Enabled = true;
                outputToolStripMenuItem.Enabled = true;
                removeAllBtn.Enabled = true;
            }  
        }

        private void fileBox_DragEnter(object sender, DragEventArgs e)
        {
            //Lets user drag files into the listbox
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                e.Effect = DragDropEffects.All;
        }

        private void removeAllBtn_Click(object sender, EventArgs e)
        {
            fileBox.Items.Clear();
            removeBtn.Enabled = false;
            browseBtn.Enabled = false;
            processBtn.Enabled = false;
            removeAllBtn.Enabled = false;
        }

        private void processBtn_Click(object sender, EventArgs e)
        {

            ProcessStartInfo pstoEdit = new ProcessStartInfo(@"C:\Program Files\ghostgum\pstoedit\pstoedit.exe");
            for (int pos = 0; pos < fileBox.Items.Count; pos++)
            {
                string inName = fileBox.Items[pos].ToString();
                try
                {
                    switch (typeSelect.SelectedIndex)
                    {
                        case 0:
                            Conversions.pdf(inName, outLabel.Text);
                            break;
                        case 1:
                            Conversions.print(inName);
                            break;
                        case 2:
                            Conversions.tiff(inName, outLabel.Text);
                            break;
                        case 3:
                            Conversions.jpeg(inName, outLabel.Text);
                            break;
                        case 4:
                            Conversions.jpeg(inName, outLabel.Text);
                            break;
                        case 5:
                            Conversions.tiff(inName, outLabel.Text);
                            break;
                        case 6:
                            Conversions.pdf(inName, outLabel.Text);
                            break;
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Failed to convert " + inName);
                    fileBox.SelectedIndex = pos;
                    safeExit = false;
                }
                if (safeExit)
                {
                    MessageBox.Show("Conversion Successful.");
                    try
                    {
                        foreach (string f in Directory.GetFiles("C:\\", "temp.*"))
                            new FileInfo(f).Delete();
                    }
                    catch
                    {
                        Close();
                    }
                        Close();
                }
            }
        }
    }
}
